import { defineStore } from 'pinia';
import { reactive, ref } from 'vue';

export const useGroupStore = defineStore('groups', () => {
  // 群聊列表
  const groups = ref([
    {
      id: '1001',
      name: '技术讨论群',
      avatar: 'https://picsum.photos/50/50?random=1',
      members: ['Alice', 'Bob', 'Charlie', 'David'], // 群成员
      owner: 'Alice', // 群主
      messages: [
        { sender: 'Bob', content: '大家好，我是新来的！', time: '昨天 14:30' },
        { sender: 'Charlie', content: '欢迎欢迎！', time: '昨天 14:31' },
        { sender: 'Alice', content: '这是我们的技术讨论群，请大家遵守群规。', time: '昨天 14:32' }
      ],
      unreadCount: 3,
      isMuted: false // 是否静音
    },
    {
      id: '1002',
      name: '产品交流群',
      avatar: 'https://picsum.photos/50/50?random=2',
      members: ['Alice', 'Eve', 'Frank'],
      owner: 'Eve',
      messages: [
        { sender: 'Eve', content: '下周三的产品会议大家都有空吗？', time: '今天 09:15' },
        { sender: 'Frank', content: '我没问题', time: '今天 09:16' }
      ],
      unreadCount: 2,
      isMuted: true // 已静音
    }
  ]);

  // 添加群聊
  const addGroup = (group) => {
    const exists = groups.value.find(g => g.name === group.name);
    if (!exists) {
      groups.value.push(group);
    }
  };

  // 删除群聊
  const deleteGroup = (groupId) => {
    const index = groups.value.findIndex(g => g.name === groupId);
    if (index !== -1) {
      groups.value.splice(index, 1);
    }
  };

  // 设置群聊未读消息数
  const setUnreadCount = (groupId, count) => {
    const group = groups.value.find(g => g.name === groupId);
    if (group) {
      group.unreadCount = count;
    }
  };

  // 置顶群聊
  const pinGroup = (groupId) => {
    const index = groups.value.findIndex(g => g.name === groupId);
    if (index !== -1) {
      const pinnedGroup = groups.value.splice(index, 1)[0];
      groups.value.unshift(pinnedGroup);
    }
  };

  // 添加群成员
  const addMember = (groupId, member) => {
    const group = groups.value.find(g => g.name === groupId);
    if (group && !group.members.includes(member)) {
      group.members.push(member);
    }
  };

  // 移除群成员
  const removeMember = (groupId, member) => {
    const group = groups.value.find(g => g.name === groupId);
    if (group) {
      group.members = group.members.filter(m => m !== member);
    }
  };

  // 发送群消息
  const sendMessage = (groupId, sender, content,username) => {
    const group = groups.value.find(g => g.id === groupId);
    if (group) {
      const newMessage = {
        sender,
        content,
        time: new Date().toLocaleTimeString()
      };
      group.messages.push(newMessage);
      
      // 如果不是当前用户发送的消息，且群未静音，则增加未读计数
      if (sender !== username && !group.isMuted) {
        group.unreadCount++;
      }
    }
  };

  // 切换群静音状态
  const toggleMute = (groupId) => {
    const group = groups.value.find(g => g.name === groupId);
    if (group) {
      group.isMuted = !group.isMuted;
    }
  };

  // 清空未读消息
  const clearUnread = (groupId) => {
    const group = groups.value.find(g => g.name === groupId);
    if (group) {
      group.unreadCount = 0;
    }
  };

  return {
    groups,
    addGroup,
    deleteGroup,
    setUnreadCount,
    pinGroup,
    addMember,
    removeMember,
    sendMessage,
    toggleMute,
    clearUnread
  };
});